"""
    链接：
        https://www.amazon.com/s?crid=3GGG12CHU8XMR&i=aps&k=evil%20eye%20bracelets&ref=nb_sb_noss_2&sprefix=evil%20eye%20bracelets%2Caps%2C318&url=search-alias%3Daps
    parsel：帮助解析数据的第三方模块
    步骤：
        1、发送网络请求
        2、获取数据
        3、解析数据
        4、保存数据
"""

import requests
import parsel
import csv

f = open('evil.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.writer(f)
csv_writer.writerow(['title', 'img_url'])

url = "https://www.amazon.com/s?crid=3GGG12CHU8XMR&i=aps&k=evil%20eye%20bracelets&ref=nb_sb_noss_2&sprefix=evil%20eye%20bracelets%2Caps%2C318&url=search-alias%3Daps"
# 伪装
headers = {
    # 用户的身份信息
    "Cookie": "session-id=140-1414687-5803049; session-id-time=2082787201l; i18n-prefs=USD; lc-main=zh_CN; skin=noskin; ubid-main=133-7726480-3893050; session-token=PUHAx3yLgE3SRcq3PEgTmVHNoDYEpBe6/8rPGyXQRZEkYYVneZDh7Gkr/zJAo3aPF/AI47SrgaMg1J4UWMazjCuMvTX2UrwQ8DkhIX/geFQjG258MzGXdVpEP+iuIE4OhN+Z74Nv666ZwaqEG+RzgV8X5RE3d7Jlx/LK7JiCCNaFe6OhixVrsVBOfkm56md1PhIk9VKGed/hHBBNIDymlSHx582O2r0K9hmfKvhdhW4=; csm-hit=tb:MV004R53C56RECEZ8K97+s-2TYS40HXQSJYK5PEPDYB|1689943758718&t:1689943758718&adb:adblk_no",
    # 防盗链
    "Referer": "https://www.amazon.com/s?k=evil+eye+bracelets&ref=nb_sb_noss",
    # 浏览器的基本信息
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
}
# 1、发送网络请求
response = requests.get(url=url, headers=headers)
# 2、获取数据
# print(response.text)
data_html = response.text
# with open('amz.html', mode='w', encoding='utf-8') as o_file:
#     o_file.write(response.content.decode())
# 3、解析数据
# css/xpath/re/json lxml/bs4/parsel
# 结构化数据 {"": "", "": ""} json 字典取值 /re
# 非结构化数据 网页源代码 css/xpath/re(模块里面的功能)   lxml/bs4/parsel/re(模块)
select = parsel.Selector(data_html)
# 每个商品的信息
# divs = select.css('div.s-main-slot.s-result-list.s-search-results.sg-row>div')
divs = select.css('.a-section.a-spacing-base')
i = 0
for div in divs:
    # i = i + 1
    # ::attr()取属性的值
    # 图片路径
    img_url = div.css('img.s-image::attr(src)').get()
    # if img_url:
    #     print(img_url)
    # 提取标签文本内容 ::text
    title = div.css('span.a-size-base-plus.a-color-base.a-text-normal::text').get()
    # csv_writer.writerow([title, img_url])
    if title:
        i = i+1
        # print(title)
        csv_writer.writerow([title, img_url])
print(f"总计{i}")
# 4、保存数据
